<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin');
$mainframe->registerEvent( 'onPrepareContent', 'plgContentmmssplr' );

function plgContentmmssplr(&$row, &$params, $page=0 ) {
	// simple performance check to determine whether bot should process further
	$db =& JFactory::getDBO();
	// simple performance check to determine whether bot should process further
	//if ( JString::strpos( $row->text, 'component' ) === false ) { 		return true; 	}
	// define the regular expression for the bot
	$plugin =& JPluginHelper::getPlugin('content', 'componentbot');
	$regex = "#{mssplr}(.*?){/mssplr}#s";
    $pluginParams = new JParameter( $plugin->params );


	// check whether mambot has been unpublished
	if ( !$pluginParams->get( 'enabled', 1 ) ) {
	$row->text = preg_replace( $regex, '', $row->text );
    return true;
     }

	// perform the replacement	
	$row->text = preg_replace_callback( $regex, 'botComponentCode_replacer2', $row->text );	
	return true;
}


function botComponentCode_replacer2( &$matches ) {
global $mainframe;
global $database ;
	$query = "SELECT params"
	. "\n FROM #__plugins"
	. "\n WHERE element = 'mmssplr'"
	. "\n AND folder = 'content'"
	;

// Connecting to database...
	$database=&JFactory::getDBO();
	$database->setQuery( $query );
	$plugin =& JPluginHelper::getPlugin('content', 'mssplr');
 	$pluginParams = new JParameter( $plugin->params );

	$rootfolder = $pluginParams->get( 'rootfolder', '' );
	$LiveSite = JURI::base();

	$sspurl =  $pluginParams->get( 'sspurl', '' );
	$ssppath = $pluginParams->get( 'ssppath', '' );
	
	//echo $sspurl ; 
	//echo $ssppath; 
	
// Separating arguments from content call	
// gallery|album,display_id,width,length,allowfullscreen
	
  $arguments = explode(',',$matches[1]);
  $path = $arguments[0];
  $display = $arguments[0];
  $display_id = $arguments[1];
  $width = $arguments[2];
  $height = $arguments[3];
  $allowfullscreen = $arguments[4];
  
  //Random string for multiple instances of mmssplr
  $id = rand_str2();
  
  //Building the complete path to gallery folder
  $gallerypath = $LiveSite.$rootfolder.'/'.$path;

  //Code to display the flash movie
  // org    '<script type="text/javascript" src="http://ssp.ms-vib.de/slideshowpro/m/embed.js"></script>
  
    $para = '<script type="text/javascript" src="http://'.$sspurl.'/'.$ssppath.'/m/embed.js"></script>
  
<div id="'.$display.'-'.$display_id.'">

</div>

<script type="text/javascript">
	SlideShowPro({
		attributes: {
			id: "'.$display.'-'.$display_id.'",
			width:  "'.$width.'",
			height: "'.$height.'"
		},
		mobile: {
			auto: false,
			poster: "vignette"
		},
		params: {
			bgcolor: "#000000",
			allowfullscreen: '.$allowfullscreen.'
		},
		flashvars: {
			xmlFilePath: "http://'.$sspurl.'/'.$ssppath.'/images.php?'.$display.'='.$display_id.'",
			paramXMLPath: "http://'.$sspurl.'/'.$ssppath.'/m/params/techno.xml",
			displayMode: "Manual",
			videoAutoStart: "Off",
			feedbackPreloaderAppearance: "Pie Spinner"
		}
	});
</script>';

	return $para;
}

// Generate a random character string
function rand_str2($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    // Length of character list
    $chars_length = (strlen($chars) - 1);

    // Start our string
    $string = $chars{rand(0, $chars_length)};
    
    // Generate random string
    for ($i = 1; $i < $length; $i = strlen($string))
    {
        // Grab a random character from our list
        $r = $chars{rand(0, $chars_length)};
        
        // Make sure the same two characters don't appear next to each other
        if ($r != $string{$i - 1}) $string .=  $r;
    }
    
    // Return the string
    return $string;
}
?>
